<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/11/9
 * Time: 18:37
 */

namespace App\Services\Jiguang;

//App\Services\Jiguang\Api::
use App\admin\Service\OA;
use App\Models\User\UserModel;
use App\Services\Api\ApiBase;
use Illuminate\Support\Facades\Redis;
use JMessage\IM\Message;
use JMessage\IM\User;
use JMessage\JMessage;
use Illuminate\Http\Request;

class JMessageApi extends ApiBase
{
    const appKey = 'ebc58098e670c47189b05396';
    const masterSecret = 'f5f03abfc9cf44b535cd5293';

    const qianzhui = '';

    public static function getjiguang()
    {
        $random_str = md5(time());
        $timestamp = time() . '123';
        $signature = md5('appkey=' . self::appKey
            . '&timestamp=' . $timestamp . '&random_str=' . $random_str . '&key=' . self::masterSecret);

        $user = OA::$user->getCacheModel();

        return [
            self::appKey,
            $random_str,
            $timestamp,
            $signature,
            $user['jiguang_id'],
            $user['jiguang_pass']
        ];
    }

    public static function getclient()
    {
        return new JMessage(self::appKey, self::masterSecret, ['disable_ssl' => true]);
    }

    /**
     * 修改用户昵称
     */
    public static function upNameNick($uid, $name)
    {
        $client = self::getclient();
        $user = new User($client);
        return $user->update($uid, [
            'nickname' => $name
        ]);
    }

    /**
     * 修改密码
     */
    public static function updatePassword($uid, $password)
    {
        $client = self::getclient();
        $user = new User($client);
        return $user->updatePassword($uid, $password);
    }


    /**
     * 注册极光用户
     */
    public static function register($uid, $password)
    {
        $client = self::getclient();
        $user = new User($client);
        return $user->register($uid, $password);
    }


    /**
     * 产生一个极光id
     */
    public static function getRandChar()
    {
        return 'mc_' . md5(rand(100, 99999999) . time());
    }


    /**
     * 使用系统账号向用户发送消息
     */
    public static function SendMsg($uid, $msg)
    {
        $jiguangid = self::idToJiguang($uid);
        $client = self::getclient();
        $message = new Message($client);
        /// $uid=self::qianzhui.$uid;
        $r = $message->sendText(
            '1',
            [
                'type' => 'admin',
                'id' => 'mc_1',///mc_1
                // 'name'=>'美策客服'
            ], [
            'type' => 'single',
            'id' => $jiguangid
        ], [
            'text' => $msg
        ], [

        ], [
            'offline' => 'true'
        ]);
        /**
         * $version: 版本号
         * $from: 发送者信息数组
         * $target: 接受者信息数组
         * $msg: 消息体数组
         * $notification: 自定义通知数组
         * $options: 其他选项数组
         */
    }


    /**
     * 用户id转换为极光id
     */
    public static function idToJiguang($id)
    {
        $one = UserModel::where([
            'id' => $id
        ])->select([
            'jiguang_id'
        ])->first();
        if ($one) {
            return $one->jiguang_id;
        } else {
            return null;
        }
    }


    public function getuserListhead(Request $request)
    {
        $namelist = $request->post('namelist');
        $list = explode(",", $namelist);
        $model = UserModel::wherein('jiguang_id', $list)->select([
            'jiguang_id', 'head'
        ])->get();
        $listAll = [];
        foreach ($model as $vo) {
            $listAll[$vo->jiguang_id] = $vo->getHead();
        }
        return $listAll;
    }

}